package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.domain.db.Announcement;
import com.tanhua.domain.db.PageResult;
import com.tanhua.dubbo.mapper.AnnouncementMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class AnnouncementApiImpl implements AnnouncementApi{
    //注入 AnnouncementMapper方法
    @Autowired
    private AnnouncementMapper announcementMapper;

    /**
     * 发布动态
     */
    @Override
    public PageResult<Announcement> getAnnouncements(Integer page, Integer pagesize) {

        //设置分页截取
        Page<Announcement> page1 = new Page<>(page,pagesize);
        //调用分页查询方法
        IPage<Announcement> iPage = announcementMapper.selectPage(page1, new QueryWrapper<>());
        //获取iPage的数据
        List<Announcement> records = iPage.getRecords();
        long total = iPage.getTotal();//总记录数
        long current = iPage.getCurrent();//当前的页数
        long pages = iPage.getPages();//总页数
        long size = iPage.getSize();//每页显示记录
        //将结果封装到pageResult
        PageResult<Announcement> announcementPageResult = new PageResult<>();
        announcementPageResult.setItems(records);
        announcementPageResult.setCounts((int)total);
        announcementPageResult.setPage((int)current);
        announcementPageResult.setPages((int)pages);
        announcementPageResult.setPagesize((int)size);
        return announcementPageResult;

    }


}
